﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Autodesk.Revit.DB;
using DataBase;

namespace DBService
{
	public class DbEdgeLoop : DbObject, IList<DbEdge>, ICollection<DbEdge>, IEnumerable<DbEdge>, IEnumerable
	{
		public DbEdgeLoop(List<DbEdge> edgeList)
		{
			this.mEdgeList = edgeList;
		}

		public DbEdgeLoop()
		{
		}

		public override string ToString()
		{
			string text = string.Format("({0},{1},{2};{3})", new object[]
			{
				this.mEdgeList[0].StartPt.X,
				this.mEdgeList[0].StartPt.Y,
				this.mEdgeList[0].StartPt.Z,
				this.mEdgeList[0].Bulge
			});
			for (int i = 0; i < this.mEdgeList.Count; i++)
			{
				text += string.Format("-({0},{1},{2};{3})", new object[]
				{
					this.mEdgeList[i].EndPt.X,
					this.mEdgeList[0].EndPt.Y,
					this.mEdgeList[0].EndPt.Z,
					this.mEdgeList[0].Bulge
				});
			}
			return text;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("select max(LoopId) from tbedgeloop", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				int num = 0;
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					try
					{
						num = Convert.ToInt32(dataTable.Rows[0][0]) + 1;
					}
					catch (Exception)
					{
					}
				}
				foreach (DbEdge dbEdge in this.mEdgeList)
				{
					dbEdge.AddToDb(db);
					if (db.Connection.State == ConnectionState.Closed)
					{
						db.Connection.Open();
					}
					string sql2 = string.Format("INSERT INTO tbedgeloop (EdgeId, LoopId) VALUES ({0},{1})", dbEdge.Id, num);
					db.ExecuteCommand(sql2);
				}
				this.mId = num;
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			throw new NotImplementedException();
		}

		public override void RemoveFromDb()
		{
			try
			{
				foreach (DbEdge dbEdge in this.mEdgeList)
				{
					dbEdge.RemoveFromDb();
				}
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbedgeloop WHERE LoopId = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public static DbEdgeLoop CreatetRectLoop(XYZ xyz, double dWidth, double dHeigh)
		{
			XYZ xyz2 = new XYZ(xyz.X - dWidth / 2.0, xyz.Y - dHeigh / 2.0, xyz.Z);
			XYZ xyz3 = new XYZ(xyz.X + dWidth / 2.0, xyz.Y - dHeigh / 2.0, xyz.Z);
			XYZ xyz4 = new XYZ(xyz.X + dWidth / 2.0, xyz.Y + dHeigh / 2.0, xyz.Z);
			XYZ xyz5 = new XYZ(xyz.X - dWidth / 2.0, xyz.Y + dHeigh / 2.0, xyz.Z);
			return new DbEdgeLoop(new List<DbEdge>
			{
				new DbEdge(xyz2, xyz5, 0.0),
				new DbEdge(xyz5, xyz4, 0.0),
				new DbEdge(xyz4, xyz3, 0.0),
				new DbEdge(xyz3, xyz2, 0.0)
			});
		}

		public static DbEdgeLoop CreateCircleLoop(XYZ xyz, double dDiameter)
		{
			List<DbEdge> list = new List<DbEdge>();
			XYZ xyz2 = new XYZ(xyz.X - dDiameter / 2.0, xyz.Y, xyz.Z);
			XYZ xyz3 = new XYZ(xyz.X + dDiameter / 2.0, xyz.Y, xyz.Z);
			list.Add(new DbEdge(xyz2, xyz3, 1.0));
			list.Add(new DbEdge(xyz3, xyz2, 1.0));
			return new DbEdgeLoop(list);
		}

		public static DbEdgeLoop GetFromDB(int EdgeLoopId, IDatabase db)
		{
			DbEdgeLoop result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbEdge> list = new List<DbEdge>();
				string sql = string.Format("SELECT EdgeId FROM tbedgeloop WHERE LoopId={0} order by EdgeId asc", EdgeLoopId);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						DbEdge fromDB = DbEdge.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["EdgeId"]), db);
						list.Add(fromDB);
					}
					result = new DbEdgeLoop(list)
					{
						mId = EdgeLoopId,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public int IndexOf(DbEdge item)
		{
			return this.mEdgeList.IndexOf(item);
		}

		public void Insert(int index, DbEdge item)
		{
			this.mEdgeList.Insert(index, item);
		}

		public void RemoveAt(int index)
		{
			this.mEdgeList.RemoveAt(index);
		}

		public DbEdge this[int index]
		{
			get
			{
				return this.mEdgeList[index];
			}
			set
			{
				this.mEdgeList[index] = value;
			}
		}

		public void Add(DbEdge item)
		{
			this.mEdgeList.Add(item);
		}

		public void AddLoop(DbEdgeLoop DbLoop)
		{
			foreach (DbEdge item in DbLoop)
			{
				this.mEdgeList.Add(item);
			}
		}

		public void Clear()
		{
			this.mEdgeList.Clear();
		}

		public bool Contains(DbEdge item)
		{
			return this.mEdgeList.Contains(item);
		}

		public void CopyTo(DbEdge[] array, int arrayIndex)
		{
			this.mEdgeList.CopyTo(array, arrayIndex);
		}

		public int Count
		{
			get
			{
				return this.mEdgeList.Count;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return true;
			}
		}

		public bool Remove(DbEdge item)
		{
			return this.mEdgeList.Remove(item);
		}

		public IEnumerator<DbEdge> GetEnumerator()
		{
			return this.mEdgeList.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return this.mEdgeList.GetEnumerator();
		}

		private List<DbEdge> mEdgeList = new List<DbEdge>();
	}
}
